#include "showmyticket.h"
#include "ui_showmyticket.h"

showmyticket::showmyticket(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::showmyticket)
{
    ui->setupUi(this);
    QString id;
    id=Form::now_account_id;
    QSqlQuery query;
    query.prepare("SELECT b.订单号,a.车次号,a.出发站,a.目的站,a.出发时间,a.到达时间,"
                  "a.全程时长,a.车票种类,a.票价,b.车厢号,b.座位号,身份证号,b.姓名 "
                  "FROM 余票信息表 a,订票单表 b "
                  "where a.`出发时间`=b.`出发时间` and a.`车次号`=b.`车次号` "
                  "and a.`车票种类`=b.`车票种类` and b.`身份证号`=:id "
                  "ORDER BY a.出发时间,a.车次号,b.车厢号,b.座位号; ");
    query.bindValue(":id", id);
    query.exec();
    // 设置列数
    int columnCount = query.record().count();
    ui->tableWidget->setColumnCount(columnCount);
    // 设置表头
    QStringList headers;
    for (int i = 0; i < columnCount; ++i) {
        headers << query.record().fieldName(i);

    }
    ui->tableWidget->setHorizontalHeaderLabels(headers);

    // 循环遍历查询结果，填充数据到表格中
    int rowCount = 0;
    while (query.next()) {
        ui->tableWidget->insertRow(rowCount);

        for (int column = 0; column < columnCount; ++column) {
            QTableWidgetItem* item = new QTableWidgetItem(query.value(column).toString());
            ui->tableWidget->setItem(rowCount, column, item);
        }

        ++rowCount;
    }
    // 获取水平表头对象
    QHeaderView* header = ui->tableWidget->horizontalHeader();
    // 设置最后一列自动拉伸
    header->setStretchLastSection(true);
    header->setSectionResizeMode(QHeaderView::Fixed);
    // 调整列宽以适应内容
    for (int column = 0; column < ui->tableWidget->columnCount()-1; ++column) {
        ui->tableWidget->resizeColumnToContents(column);
    }
    // 设置表格的编辑属性为NoEditTriggers
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
     ui->tableWidget->verticalHeader()->setVisible(false);

    QString styleSheet = "QTableWidget { background-color: #ffffff; }"
                         "QTableWidget::item { background-color: #f5f5f5; }"
                         "QTableWidget::item:alternate { background-color: #ffffff; }"
                         "QTableWidget::item:selected { background-color: #ff69b4; }"
                         "QTableWidget::item:hover { background-color: #e0e0e0; }"
                         "QHeaderView::section { background-color: #a8c5ff; color: #ffffff; border: none; }"
                         "QHeaderView::section:hover { background-color: #9bb4ff; }";

    ui->tableWidget->setStyleSheet(styleSheet);
     ui->tableWidget->setAlternatingRowColors(true);
     ui->tableWidget->horizontalHeader()->setStyleSheet(styleSheet);
     ui->tableWidget->verticalHeader()->setStyleSheet(styleSheet);


    QFont font("黑体", 8);
    ui->tableWidget->setFont(font);
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);

}

showmyticket::~showmyticket()
{
    delete ui;
}

void showmyticket::on_refundButton_clicked()
{
    QList<QTableWidgetItem *> selectedItems = ui->tableWidget->selectedItems();
        // 获取各个单元格的内容
     if (!selectedItems.isEmpty())
    {QString ordernum=selectedItems[0]->text();//订单号
    QString tnum=selectedItems[1]->text();//车次号
    QString cnum=selectedItems[9]->text();//车厢号
    QString snum=selectedItems[10]->text();//座位号
     QString ttype=selectedItems[7]->text();//座位种类
    QString odatetime=selectedItems[4]->text();//出发时间
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "Title", "是否退票", QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes) {
                         //进行退票操作
        QSqlQuery query;
        if(ttype!="站票")
        {query.prepare("insert into 退票座位表 values(:tnum,:odatetime,:cnum,:snum) ");
        query.bindValue(":tnum", tnum);
        query.bindValue(":cnum", cnum);
        query.bindValue(":snum", snum);
        query.bindValue(":odatetime", odatetime);
        query.exec();}
        query.prepare("update 余票信息表 set 余票张数=余票张数+1 where 车次号=:tnum and 车票种类=:ttype and 出发时间=:odatetime");
        query.bindValue(":tnum", tnum);
        query.bindValue(":ttype", ttype);
        query.bindValue(":odatetime", odatetime);
        query.exec();//余票加一
        query.prepare("delete from 订票单表  where 订单号=:ordernum");
        query.bindValue(":ordernum", ordernum);
        if(query.exec())//删除订单
        {
            QMessageBox::information(this,"提示","退票成功");
            // 获取选中行的索引
            int rowIndex = ui->tableWidget->currentRow();
            // 删除选中行
            ui->tableWidget->removeRow(rowIndex);
        }
    }
    else {
        // 用户取消退票
    }}
}

void showmyticket::on_exitButton_clicked()
{
    emit sendsignal();
       this->close();
}
